﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using SoftwareLogistics.SharepointClient.Models;
using SoftwareLogistics.SharepointClient.Library;
using System.Diagnostics;
using SoftwareLogistics.SharepointClient.Library.Models;

namespace SoftwareLogistics.SharepointClient.Silverlight.Views
{
    public partial class Main : Page
    {
        Services.SPListService _listService;

        public Main()
        {
            InitializeComponent();

            _listService = new Services.SPListService();
            _listService.GotListRows += (sender, args) =>
            {
                switch (args.ListMetaData.Name)
                {
                    case "Tasks": TasksList.ItemsSource = args.Rows; break;
                    case "Time": TimeList.ItemsSource = args.Rows; break;
                    case "Project": ProjectList.ItemsSource = args.Rows; break;
                }
            };

            ProjectFilter.ItemsSource = Common.Filters["Project"];
            TaskFilters.ItemsSource = Common.Filters["Tasks"];
            TimeFilters.ItemsSource = Common.Filters["Time"];
        }

        private string GetFilterName(string listName)
        {
            return string.Format("{0}Filter", listName);
        }
        
        private void SetDefaultFilter(ComboBox comboBox)
        {
            var listName = comboBox.Tag.ToString();
            var filterName = GetFilterName(listName);
            if (Common.StateBag.ContainsKey(filterName))
                comboBox.SelectedItem = Common.Filters[listName].First(filter => filter.Name == Common.StateBag[filterName]);
            else
                comboBox.SelectedItem = Common.Filters[listName].First(filter => filter.IsDefault == true);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            SetDefaultFilter(TimeFilters);
            SetDefaultFilter(TaskFilters);
            SetDefaultFilter(ProjectFilter);
        }

        private void Row_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Debug.WriteLine("SELECTED ITEM!");

            if (e.AddedItems.Count == 1)
            {
                var spFormData = new SPFormData();
                spFormData.RowData = e.AddedItems[0] as SPDataRow;
                spFormData.IsEdit = true;
                spFormData.ListName = spFormData.RowData.ListName;
                
                var popup = new Views.PopupDataForm();
                popup.Show(spFormData);
                popup.Closed += (sndr, args) =>
                {

                };                
            }
        }

        private void AddTimeButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            var task = (e.OriginalSource as Image).Tag as SPDataRow;

            var spFormData = new SPFormData();
            spFormData.IsEdit = false;
            spFormData.ListName = "Time";
            spFormData.FormDefaults.AddValue("Task", string.Format("{0};#{1}", task.ID, task.Title));
            spFormData.FormDefaults.AddValue("User", Common.Users.CurrentUser.ID.ToString());
            spFormData.FormDefaults.AddValue("Is Billable", "TRUE");
            spFormData.FormDefaults.AddValue("Work Date", Common.GetSPDateFormat(DateTime.Today));
            spFormData.FormDefaults.AddValue("Hours Worked", "0");

            var popup = new Views.PopupDataForm();
            popup.Show(spFormData);
        }

        private void AddTaskButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            var project = (e.OriginalSource as Image).Tag as SPDataRow;

            var spFormData = new SPFormData();
            spFormData.IsEdit = false;
            spFormData.ListName = "Tasks";
            spFormData.FormDefaults.AddValue("Project", string.Format("{0};#{1}", project.ID, project.Title));

            var popup = new Views.PopupDataForm();
            popup.Show(spFormData);
        }

        private void Filter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 1)
            {
                var listName = ((ComboBox)sender).Tag.ToString();
                var query = e.AddedItems[0] as SPQuery;
                _listService.GetListRows(listName, query);
                Common.StateBag[GetFilterName(listName)] = query.Name;
                Common.SaveStateBag();
            }
        }
    }
}
